﻿using System.Linq.Expressions;
using Basic.HrDAL;
using Basic.HrModel.DB;
using Basic.HrModel.EmpPost;
using WeDonekRpc.Helper;

namespace Basic.HrCollect.Impl
{
    internal class EmpPostCollect : IEmpPostCollect
    {
        private readonly IEmpPostDAL _EmpPost;

        public EmpPostCollect ( IEmpPostDAL empPost )
        {
            this._EmpPost = empPost;
        }
        public void Clear ( long empId )
        {
            this._EmpPost.Clear(empId);
        }
        public long Add ( EmpPostAddDto add )
        {
            if ( this._EmpPost.IsExists(a => a.EmpId == add.EmpId && a.DeptId == add.DeptId && a.PostCode == add.PostCode) )
            {
                throw new ErrorException("hr.emp.post.repeat");
            }
            return this._EmpPost.Add(add);
        }
        public void Delete ( DBEmpPost obj )
        {
            this._EmpPost.Delete(obj.Id);
        }
        public T Get<T> ( long id ) where T : class
        {
            return this._EmpPost.Get<T>(id);
        }
        public DBEmpPost Get ( long id )
        {
            return this._EmpPost.Get(id);
        }

        public string[] GetPost ( long empId, long deptId )
        {
            return this._EmpPost.Gets(a => a.EmpId == empId && a.DeptId == deptId, a => a.PostCode);
        }
        public Result[] GetEmpPost<Result> ( long empId ) where Result : class, new()
        {
            return this._EmpPost.Gets<Result>(a => a.EmpId == empId);
        }
        public Result[] GetEmpPost<Result> ( long empId, long[] companyId ) where Result : class, new()
        {
            if ( !companyId.IsNull() )
            {
                return this._EmpPost.Gets<Result>(a => a.EmpId == empId && companyId.Contains(a.CompanyId));
            }
            return this._EmpPost.Gets<Result>(a => a.EmpId == empId);
        }
        public Result[] GetEmpPost<Result> ( long empId, long companyId ) where Result : class, new()
        {
            return this._EmpPost.Gets<Result>(a => a.EmpId == empId && a.CompanyId == companyId);
        }
        public Dictionary<long, string[]> GetEmpDeptPost ( long[] empId, long deptId )
        {
            return this._EmpPost.Gets(a => empId.Contains(a.EmpId) && a.DeptId == deptId, a => new
            {
                a.EmpId,
                a.PostCode
            }).GroupBy(a => a.EmpId).ToDictionary(a => a.Key, a => a.Select(c => c.PostCode).ToArray());
        }
        public Result[] GetEmpDeptPost<Result> ( long empId, long deptId, Expression<Func<DBEmpPost, Result>> selector )
        {
            return this._EmpPost.Gets<Result>(a => a.EmpId == empId && a.DeptId == deptId, selector);
        }
        public Result[] GetEmpDeptPost<Result> ( long[] empId, long[] deptId, Expression<Func<DBEmpPost, Result>> selector )
        {
            return this._EmpPost.Gets<Result>(a => empId.Contains(a.EmpId) && deptId.Contains(a.DeptId), selector);
        }
        public Dictionary<long, string[]> GetEmpPost ( KeyValuePair<long, long>[] empAndDept )
        {
            return this._EmpPost.GetEmpPost(empAndDept);
        }
        public EmpPostDto[] GetEmpPost ( long[] empId, long companyId )
        {
            return this._EmpPost.Gets<EmpPostDto>(a => empId.Contains(a.EmpId) && a.CompanyId == companyId);
        }

        public bool CheckIsExists ( long empId, long comId )
        {
            return this._EmpPost.IsExists(c => c.EmpId == empId && c.CompanyId == comId);
        }
    }
}
